<?php

namespace App\Models\Account;

use App\Models\BaseModel;
use App\Helpers\Code;

class UserPermission extends BaseModel
{

    protected $table = 'user_permissions';
    protected $fillable = ['title', 'description', 'group_id', 'slug', 'http_method', 'http_path', 'is_enabled', 'data_scope'];
    public $params = ['title', 'group_id', 'slug', 'http_method', 'http_path', 'is_enabled'];
    public $sorters = ['title', 'slug', 'http_method', 'http_path', 'group_id', 'description', 'updated_at'];

    public function usergroups()
    {
        return $this->belongsToMany('App\Models\Account\UserGroup', 'user_group_map_permission', 'permission_id', 'group_id');
    }

    public function group()
    {
        return $this->belongsTo('App\Models\Account\UserPermissionCategory', 'group_id');
    }

    public function addPermission($data)
    {
        $group = UserPermissionCategory::find($data['group_id']);

        if (!$group) {
            return Code::INSIDE_EXIST_DEPENDENCE;
        }
        
        $data['data_scope'] = $this->getDataScope();

        return $this->create($data);
    }

    public function deletePermission($id)
    {
        $model = $this->with('usergroups')->find($id);

        if (!$model) {
            return null;
        } else if ($model->usergroups->count() > 0) {
            return Code::INSIDE_EXIST_DEPENDENCE;
        }

        return $model->delete();
    }

    public function editPermission($id, $data)
    {
        $model = $this->find($id);

        if (!$model) {
            return null;
        }

        $group = UserPermissionCategory::find($data['group_id']);

        if (!$group) {
            return Code::INSIDE_EXIST_DEPENDENCE;
        }

        return $model->updateOrCreate(['id' => $id], $data);
    }

    public function getPermissionList($limit, $sorter, $search)
    {
        $perPage = $limit > 0 ? $limit : $this->limit;
        $model = $this->with(['group:id,title'])->orderBy($sorter[0], $sorter[1]);
        $model->select(['id', 'group_id', 'title', 'slug', 'http_method', 'http_path', 'is_enabled', 'description', 'updated_at']);

        foreach ($search as $key => $value) {
            switch ($key) {
                case 'title':
                case 'slug':
                case 'http_path':
                    $model->where($key, 'like', '%' . $value . '%');
                    break;
                case 'http_method':
                    $model->where($key, $value);
                    break;
                case 'group_id':
                case 'is_enabled':
                    $data = explode(',', $value);
                    if (count($data) > 0) {
                        $model->whereIn($key, $data);
                    }
                    break;
            }
        }
        return $model->paginate($perPage)->toArray();
    }

    public function getPermission($id)
    {
        $model = $this->with(['group:id,title'])
                ->find($id, ['id', 'group_id', 'title', 'slug', 'http_method', 'http_path', 'is_enabled', 'description']);

        if (!$model) {
            return null;
        }

        return $model->toArray();
    }

    /**
     * 获取操作描述
     * @param $slug        操作简称
     * @return null
     */
    public function getOperationPermission($slug)
    {
        $data = $this->where('slug', $slug)->first();

        if (!$data) {
            return null;
        }

        return $data->toArray();
    }

}
